package com.itheima.comprehensivecase;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MovieService {

    private static List<Movie> movies = new ArrayList<>();
    private static int movieNum = 0;
    private Scanner sc = new Scanner(System.in);

    public void start() {
        // 1. 准备操作界面
        System.out.println("===欢迎来到电影管理系统===");
        System.out.println("1. 添加电影");
        System.out.println("2. 删除电影");
        System.out.println("3. 删除某个明星的电影");
        System.out.println("4. 查询电影");
        System.out.println("5. 查询所有电影");
        System.out.println("6. 退出系统");
        System.out.println("请输入命令：");
        while (true) {
            String command = sc.nextLine();
            switch (command) {
                case "1":
                    // 添加电影
                    addMovie();
                    break;
                case "2":
                    // 删除电影
                    deleteMovie();
                    break;
                case "3":
                    // 删除某个明星的电影
                    deleteByActorName();
                    break;
                case "4":
                    // 查询电影
                    queryMovie();
                    break;
                case "5":
                    // 查询全部电影
                    queryAllMovie();
                    break;
                case "6":
                    System.out.println("===退出系统===");
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    /**
     * 添加电影
     */
    private void addMovie() {
        System.out.println("===添加电影===");
        //每部电影是电影对象
        // 1. 输入电影名
        System.out.println("请输入电影名：");
        String name = sc.nextLine();
        if (name.length() > 20) {
            System.out.println("电影名不能超过20个字符");
            return;
        }

        // 2. 输入电影评分
        System.out.println("请输入电影评分：");
        double score = sc.nextDouble();

        while (score < 0 || score > 10) {
            System.out.println("电影评分必须在0-10之间");
            System.out.println("请输入电影评分：");
            score = sc.nextDouble();
        }

        // 3. 输入电影价格
        System.out.println("请输入电影价格：");
        int price = sc.nextInt();

        while (price < 0) {
            System.out.println("电影价格必须大于0");
            System.out.println("请输入电影价格：");
            price = sc.nextInt();
        }
        sc.nextLine();
        // 4. 输入电影演员
        System.out.println("请输入电影演员：");
        String actor = sc.nextLine();

        // 5. 创建电影对象
        Movie movie = new Movie(name, score, price, actor);
        movies.add(movie);

        System.out.println("电影添加成功");
    }

    /**
     * 格式化输出电影信息
     */
    private void queryAllMovie() {
        if (movies.isEmpty()) {
            System.out.println("暂无电影");
            return;
        }
        int nameLength = 0, scoreLength = 0, priceLength = 0, actorLength = 0;
        for (Movie movie : movies) {
            nameLength = Math.max(movie.getName().length(), nameLength);
            scoreLength = Math.max((movie.getScore() + "").length(), scoreLength);
            priceLength = Math.max((movie.getPrice() + "").length(), priceLength);
            actorLength = Math.max(movie.getActor().length(), actorLength);
        }
        System.out.print("名称");
        for (int i = 0; i < nameLength - 1; i++) {
            System.out.print(" ");
        }

        System.out.print("评分");
        for (int i = 0; i < scoreLength - 1; i++) {
            System.out.print(" ");
        }

        System.out.print("价格");
        for (int i = 0; i < priceLength - 1; i++) {
            System.out.print(" ");
        }

        System.out.print("演员");
        for (int i = 0; i < actorLength - 1; i++) {
            System.out.print(" ");
        }
        System.out.println();

        for (Movie movie : movies) {
            System.out.print(movie.getName());
            for (int i = 0; i <= nameLength - movie.getName().length(); i++) {
                System.out.print(" ");
            }

            System.out.print(movie.getScore());
            for (int i = 0; i <= scoreLength - (movie.getScore() + "").length(); i++) {
                System.out.print(" ");
            }

            System.out.print(movie.getPrice());
            for (int i = 0; i <= priceLength - (movie.getPrice() + "").length(); i++) {
                System.out.print(" ");
            }

            System.out.print(movie.getActor());
            for (int i = 0; i <= actorLength - movie.getActor().length(); i++) {
                System.out.print(" ");
            }
            System.out.println();
        }

    }

    /**
     * 格式化输出电影信息
     * return: 每块内容中最长长度
     */
    private void queryOneMovie(String name) {
        Movie movie = findMovieByName(name);
        if (movie == null) {
            System.out.println("暂无该电影");
            return;
        }

        System.out.println("名称");
        for (int i = 0; i < movie.getName().length() - 2; i++) {
            System.out.print(" ");
        }

        System.out.print("评分");
        for (int i = 0; i < (movie.getScore() + "").length() - 2; i++) {
            System.out.print(" ");
        }

        System.out.print("价格");
        for (int i = 0; i < (movie.getPrice() + "").length() - 2; i++) {
            System.out.print(" ");
        }

        System.out.print("演员");
        for (int i = 0; i < movie.getActor().length() - 2; i++) {
            System.out.print(" ");
        }
        System.out.println();
        System.out.print(movie.getName() + " ");
        System.out.print(movie.getScore() + " ");
        System.out.print(movie.getPrice() + " ");
        System.out.print(movie.getActor() + " ");


    }

    /**
     * 查询电影
     */
    private void queryMovie() {
        System.out.println("===查询电影===");
        if (movies.isEmpty()) {
            System.out.println("暂无电影");
            return;
        }
        System.out.println("请输入电影名称：");

        // 获取电影
        String name = sc.next();
        // 打印电影信息
        queryOneMovie(name);
    }


    private Movie findMovieByName(String name) {
        for (Movie movie : movies) {
            if (movie.getName().equals(name)) {
                return movie;
            }
        }
        return null;
    }

    private void deleteMovie() {
        System.out.println("===删除电影===");
        if (movies.isEmpty()) {
            System.out.println("暂无电影");
            return;
        }
        System.out.println("请输入电影名称：");
        String name = sc.next();
        for (int i = 0; i < movies.size(); i++) {
            if (movies.get(i).getName().equals(name)) {
                movies.remove(i);
                System.out.println("删除电影成功");
                return;
            }
        }
        System.out.println("暂无该电影");
        return;
    }

    private void deleteByActorName() {
        System.out.println("===删除演员相关电影===");
        if (movies.isEmpty()){
            System.out.println("暂无电影");
            return;
        }
        System.out.println("请输入演员名称：");
        String actor = sc.next();
        for (int i = 0; i < movies.size(); i++) {
            if (movies.get(i).getActor().contains(actor)) {
                movies.remove(i--);
                System.out.println("删除电影成功");
            }
        }
    }
}
